//
//  HttpTool.swift
//  FirstSwiftProduct
//
//  Created by 范泽华 on 2017/10/11.
//  Copyright © 2017年 范泽华. All rights reserved.
//

import UIKit
import Alamofire
class HttpTool: NSObject {
    
    static let shareTool:HttpTool = {
        let manager = HttpTool()
        
       return manager
    }()
    
    func requestWithURL(URLString:String,MethodType:HTTPMethod,parameters:[String:AnyObject],showHud:Bool,sucess:@escaping(String?,AnyObject?)->(),fail:@escaping(String?)->(),error:@escaping(Error?)->()) {
        
        if URLString.isEmpty {
            assert(false, "URL路径为空")
            print("url不能为空")
            return
        }
        let urlPredicate = NSPredicate.init(format: "SELF MATCHES %@", "\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?")
        let result = urlPredicate.evaluate(with: URLString)
        if !result {
            assert(false, "非法的URL")
            return
        }
            //判断网络是否连接
        
        
        
        
        let headers: HTTPHeaders = [
            "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
            "Accept": "application/json"
        ]

        
        
        
        switch MethodType {
        case .get:Alamofire.request(URLString, method:.get, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(queue: nil, options: .allowFragments) { (dataRespones) in
            
            if dataRespones.result.isSuccess {
                    if let JSON = dataRespones.result.value{
                    
                    sucess("请求成功", JSON as AnyObject)
                    
                    }
            }else if dataRespones.result.isFailure{
                fail("请求失败")
                error(dataRespones.result.error)
                }
            }
            break
        case .post :Alamofire.request(URLString, method:.post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).responseJSON(queue: nil, options: .allowFragments) { (dataRespones) in
                if dataRespones.result.isSuccess {
                    if let JSON = dataRespones.result.value{
                        sucess("请求成功", JSON as AnyObject)
                    }
                }else if dataRespones.result.isFailure{
                    fail("请求失败")
                    error(dataRespones.result.error)
                }
            }
            break

        default:break
            
        }
        
    }
    
    
    
    
    class func requestWithPost(url:String) -> Void {
//        Alamofire.request(url, method: .post, parameters: parames, encoding: .URL, headers: ["apix-key":apiKey]?)

        let headers: HTTPHeaders = [
            "Authorization": "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
            "Accept": "application/json"
        ]
        Alamofire.request(url, method: .post, parameters: [:], encoding: JSONEncoding.default, headers: headers).responseJSON { (respose) in
            if let JSON = respose.result.value {
//                print("JSON: \(JSON)")
            }
        }
        
        
        Alamofire.request(url).responseJSON { (respones) in
            if let JSON = respones.result.value {
//                print("JSON: \(JSON)")
            }else{
                print("woowowowowow")
            }
        }
    }
    
    
    
    func requestJsonDataWithUrl(Type:HTTPMethod,
                                url:String,
                                parames:[String:AnyObject],
                                succeed:@escaping(String?, AnyObject?)->(),
                                error:@escaping(NSError?)->(),
                                failure:@escaping(String?)->() )  {
        
        Alamofire.request(url, method: Type, parameters: parames).responseJSON { (requestback) in
            
            switch requestback.result{
                
            case .success:
                if let value = requestback.result.value{
                    succeed("yes", value as AnyObject)
                }
                
            case .failure:
                failure("fail")
            }
        }
        
//        Alamofire
    }
   
    
//    func request(Type:HTTPMethod,
//                 url:String,
//                 parames:[String:AnyObject],
//                 succeed:@escaping(String?, AnyObject?)->(),
//                 failure:@escaping(String?)->() ){
//        
//        
//        Alamofire.request(url, method:Type, parameters:parames).responseJSON { (retultOb) in
//            
//            switch retultOb.result {
//                
//            //成功
//            case .success:
//                
//                if let value = retultOb.result.value{
//                    // 成功闭包
//                    succeed("succeed", value as AnyObject)
//                }
//                
//            //失败
//            case .failure:
//                
//                failure("failure")
//            }
//        }

        
//    }
    
    
    
    class func block(succs:(String?,AnyObject?)->()) -> Void {
        succs("su", "value" as AnyObject)
    }
}

/**
 

 */
